package com.zoshine.zzg.service;

import com.github.pagehelper.util.StringUtil;
import com.zoshine.zzg.dao.OrganizationDao;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.Organization;
import com.zoshine.zzg.pojo.pojostatus.CommonStatus;
import com.zoshine.zzg.util.idworker.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 组织ServiceImpl
 *
 * @author 田沛凌
 * @time 2017年6月22日 上午10:22:02
 */
@Service
public class OrganizationService extends BaseService<Organization> {
    private IdWorker<Long> orgIdWork;
    private OrganizationDao organizationDao;

    @Autowired
    public void setOrganizationDao(OrganizationDao organizationDao) {
        this.organizationDao = organizationDao;
    }

    @Autowired
    public void setOrgIdWork(IdWorker<Long> orgIdWork) {
        this.orgIdWork = orgIdWork;
    }
    public Organization add(Organization organization) {
        //非空验证
        if (StringUtil.isEmpty(organization.getOrgName())) {
            throw new AppException(ErrorMessage.ORG_NAME_CAN_NOT_NULL);
        }
        if (null == organization.getOrgType()) {
            throw new AppException(ErrorMessage.ORG_TYPE_CAN_NOT_NULL);
        }
        //值验证
        if (organization.getOrgName().length() > 60) {
            throw new AppException(ErrorMessage.ORG_NAME_CAN_NOT_OVER_60);
        }

        //组织名称重复验证
        Organization check = organizationDao.selectByName(organization.getOrgName());
        if (check != null) {
            throw new AppException(ErrorMessage.ORG_NAME_CAN_NOT_REPEAT);
        }
        organization.setId(orgIdWork.createId());
        //组织状态
        if (organization.getOrgStatus() == null) {
            organization.setOrgStatus(CommonStatus.VALID.getValue());
        }
        //暂设银行名，卡号，余额
        organization.setBankName("中国农业银行");
        organization.setBankCard(6228480402564890000L);
        organization.setBalance(0);
        organization.setCreateTime(new Date());
        organization.setUpdateTime(new Date());
        organizationDao.insert(organization);
        return  organization;
    }
//
//    @Override
//    public void delOrg(Long orgId) {
//        if (orgId == null) {
//            throw new AppException(ErrorMessage.ORG_ID_CAN_NOT_NULL);
//        }
//        organizationDao.delete(orgId);
//    }
//
//    @Override
//    public Organization getOrg(Long orgId) {
//        if (orgId == null) {
//            throw new AppException(ErrorMessage.ORG_ID_CAN_NOT_NULL);
//        }
//        return organizationDao.selectOne(orgId);
//    }
//
//    @Override
//    public void editOrg(Organization organization) {
//        //非空验证
//        if (null == organization.getOrgId()) {
//            throw new AppException(ErrorMessage.ORG_ID_CAN_NOT_NULL);
//        }
//        if (StringUtil.isEmpty(organization.getOrgName())) {
//            throw new AppException(ErrorMessage.ORG_NAME_CAN_NOT_NULL);
//        }
//        if (null == organization.getOrgType()) {
//            throw new AppException(ErrorMessage.ORG_TYPE_CAN_NOT_NULL);
//        }
//        //值验证
//        if (organization.getOrgName().length() > 60) {
//            throw new AppException(ErrorMessage.ORG_NAME_CAN_NOT_OVER_60);
//        }
//
//        // 名称重复验证
//        Organization check = organizationDao.selectByName(organization.getOrgName());
//        if (check != null && !Objects.equals(check.getOrgId(), organization.getOrgId())) {
//            throw new AppException(ErrorMessage.ORG_NAME_CAN_NOT_REPEAT);
//        }
//        organization.setUpdateTime(new Date());
//        organizationDao.update(organization);
//    }
//
//    @Override
//    public List<Organization> listOrg(Organization organization) {
//        return organizationDao.selectList(organization);
//    }

}
